package at.fhooe.engine.physics;

import java.awt.Graphics2D;

public class CircleBody extends PhysicBody {

	private double m_radius;
	
	public CircleBody(String name, double radius, boolean isEntity) {
		super(name, isEntity);
		m_radius = radius;
	}

	@Override
	public boolean isColliding(PhysicBody physicBody) {
		return physicBody.isColliding(this);
	}

	@Override
	public boolean isColliding(CircleBody circleBody) {
		double deltaX = getPositionX() - circleBody.getPositionX();
		double deltaY = getPositionY() - circleBody.getPositionY();
		double radius = getRadius() + circleBody.getRadius();
		
		return deltaX * deltaX + deltaY * deltaY < radius * radius;
	}

	public double getRadius() {
		return m_radius;
	}

	@Override
	public boolean isColliding(SquareBody squareBody) {
		double rectLeft = squareBody.getPositionX();
		double rectRight = squareBody.getPositionX()+squareBody.getWidth();
		double rectTop = squareBody.getPositionY();
		double rectBottom = squareBody.getPositionY() + squareBody.getHeight();
		
		double closestX = Math.max(rectLeft, Math.min(rectRight, this.getPositionX()));
		double closestY = Math.max(rectTop, Math.min(rectBottom, this.getPositionY()));
		
		double distanceX = this.getPositionX() - closestX;
		double distanceY = this.getPositionY() - closestY;
		
		double distanceSquared = distanceX * distanceX + distanceY * distanceY;
		return distanceSquared < (m_radius * m_radius);
	}

}
